using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Northwind.NET.FE.Reports;
 
namespace Northwind.NET.FE.Forms
{

    public partial class OrdersForm : Form
    {
        public OrdersForm()
        {
            InitializeComponent();
        }

        private void OrdersForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Northwind.NET.FE.Globals.Navigator.UnhideMainSwitchboard();
        }

        private void orderBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.orderBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.ordersFormDataSet);
        }

        private void OrdersForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'ordersFormDataSet.Shipper' table. You can move, or remove it, as needed.
            this.shipperTableAdapter1.Fill(this.ordersFormDataSet.Shipper);
            // TODO: This line of code loads data into the 'dataModelDataSet.Shipper' table. You can move, or remove it, as needed.
            this.shipperTableAdapter.Fill(this.dataModelDataSet.Shipper);
            // TODO: This line of code loads data into the 'ordersFormDataSet.CustomerOrdersProducts' table. You can move, or remove it, as needed.
            this.customerOrdersProductsTableAdapter.Fill(this.ordersFormDataSet.CustomerOrdersProducts);
            // TODO: This line of code loads data into the 'ordersFormDataSet.CustomerOrderTotals' table. You can move, or remove it, as needed.
            this.customerOrderTotalsTableAdapter.Fill(this.ordersFormDataSet.CustomerOrderTotals);
            // TODO: This line of code loads data into the 'ordersFormDataSet.Employee' table. You can move, or remove it, as needed.
            this.employeeTableAdapter.Fill(this.ordersFormDataSet.Employee);
            // TODO: This line of code loads data into the 'ordersFormDataSet.Customer' table. You can move, or remove it, as needed.
            this.customerTableAdapter.Fill(this.ordersFormDataSet.Customer);
            // TODO: This line of code loads data into the 'ordersFormDataSet.Order' table. You can move, or remove it, as needed.
            this.orderTableAdapter.Fill(this.ordersFormDataSet.Order);

            setBillToValues();
        }

        private void billToComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            setBillToValues();
            setShipViaValues();
        }

        private void orderBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            setBillToValues();
        }

        private void setBillToValues()
        {
            ComboBox comboBox = billToComboBox;
            if (comboBox.SelectedValue == null) return;
            
            int customerId = (int)comboBox.SelectedValue;
            
            Northwind.NET.FE.Datasets.OrdersFormDataSet.CustomerRow customer =
             this.ordersFormDataSet.Customer.FindByID(customerId);

            billToAddressTextBox.Text = customer.Address ;
            billToRegionTextBox.Text = customer.Region;
            billToCityTextBox.Text = customer.City;
            billToPostalCodeTextBox.Text = customer.PostalCode;
            billToCountryTextBox.Text = customer.Country;
        }
        private void setShipViaValues()
        {
            ComboBox comboBox = billToComboBox;
            if (comboBox.SelectedValue == null) return;

            int customerId = (int)comboBox.SelectedValue;

            Northwind.NET.FE.Datasets.OrdersFormDataSet.CustomerRow customer =
             this.ordersFormDataSet.Customer.FindByID(customerId);

            shipNameTextBox.Text = customer.Name;
            shipAddressTextBox.Text = customer.Address;
            shipRegionTextBox.Text = customer.Region;   
            shipCityTextBox.Text = customer.City;
            shipPostalCodeTextBox.Text = customer.PostalCode;
            shipCountryTextBox.Text = customer.Country;       
        }

        private InvoiceReport _report;
        private void printInvoiceButton_Click(object sender, EventArgs e)
        {
            _report = new InvoiceReport();
            _report.PreviewInvoice(Int32.Parse(iDTextBox.Text));
        }

    }
}